﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using RiskMan.DataClass.Models.Sterling;

namespace RiskMan.Views.Streling
{
    /// <summary>
    /// Interaction logic for AddSymbolCodeInstrument.xaml
    /// </summary>
    public partial class AddSymbolCodeInstrument : Window
    {
        public AddSymbolCodeInstrument()
        {
            InitializeComponent();
            dtgdSterlingSymbol.ItemsSource = MainWindow.StaticLevel.DataProvider.SterlingSymbols.OrderBy(a => a.Symbol);
        }

        private void Button_AddRow_Click(object sender, RoutedEventArgs e)
        {
            if (MainWindow.StaticLevel.DataProvider.SterlingSymbols.Count(a => a.Symbol == TxbxSymbol.Text) == 0)
                MainWindow.StaticLevel.DataProvider.SterlingSymbols.Add(new SterlingSymbol() { IsCheck = false, Symbol = TxbxSymbol.Text });

            dtgdSterlingSymbol.ItemsSource = MainWindow.StaticLevel.DataProvider.SterlingSymbols.OrderBy(a => a.Symbol);
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < MainWindow.StaticLevel.DataProvider.SterlingSymbols.Count; i++)
            {
                if (MainWindow.StaticLevel.DataProvider.SterlingSymbols[i].IsCheck)
                {
                    MainWindow.StaticLevel.DataProvider.SterlingSymbols.RemoveAt(i);
                    i--;
                }
            }

            dtgdSterlingSymbol.ItemsSource = MainWindow.StaticLevel.DataProvider.SterlingSymbols.OrderBy(a => a.Symbol);
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in MainWindow.StaticLevel.DataProvider.SterlingSymbols)
            {
                var isChecked = ((CheckBox)sender).IsChecked;
                if (isChecked != null) item.IsCheck = (bool)isChecked;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MainWindow.StaticLevel.DataProvider.stiQuote.DeRegisterAllQuotes();
            foreach (var item in MainWindow.StaticLevel.DataProvider.SterlingSymbols)
            {
                MainWindow.StaticLevel.DataProvider.stiQuote.RegisterQuote(item.Symbol, "");
            }
        }
    }
}
